﻿using System;
using Microsoft.Xna.Framework;

namespace Firefly.Framework.Input
{
    public class GamePadManagerCollection : IUpdateable
    {
        public GamePadManager GamePadOne { get; private set; }
        public GamePadManager GamePadTwo { get; private set; }
        public GamePadManager GamePadThree { get; private set; }
        public GamePadManager GamePadFour { get; private set; }

        public GamePadManagerCollection()
        {
            Enabled = true;
            UpdateOrder = 0;
            GamePadOne = new GamePadManager(PlayerIndex.One);
            GamePadTwo = new GamePadManager(PlayerIndex.Two);
            GamePadThree = new GamePadManager(PlayerIndex.Three);
            GamePadFour = new GamePadManager(PlayerIndex.Four);
        }

        public GamePadManager GetGamePad(PlayerIndex playerIndex)
        {
            switch (playerIndex)
            {
                case PlayerIndex.One:
                    return GamePadOne;
                case PlayerIndex.Two:
                    return GamePadTwo;
                case PlayerIndex.Three:
                    return GamePadThree;
                case PlayerIndex.Four:
                    return GamePadFour;
            }

            return null;
        }

        #region IUpdateable Members

        public event EventHandler EnabledChanged;
        public event EventHandler UpdateOrderChanged;

        private bool _enabled;
        private int _updateOrder;

        public bool Enabled
        {
            get { return _enabled; }
            set
            {
                if (value != _enabled)
                {
                    _enabled = value;
                    NotifyEnabledChanged();
                }
            }
        }
        public int UpdateOrder
        {
            get { return _updateOrder; }
            set
            {
                if (value != _updateOrder)
                {
                    _updateOrder = value;
                    NotifyUpdateOrderChanged();
                }
            }
        }

        private void NotifyEnabledChanged()
        {
            EventHandler handler = EnabledChanged;

            if (handler != null)
            {
                handler(this, new EventArgs());
            }
        }

        private void NotifyUpdateOrderChanged()
        {
            EventHandler handler = UpdateOrderChanged;

            if (handler != null)
            {
                handler(this, new EventArgs());
            }
        }

        public void Update(GameTime gameTime)
        {
            if (Enabled)
            {
                GamePadOne.Update(gameTime);
                GamePadTwo.Update(gameTime);
                GamePadThree.Update(gameTime);
                GamePadFour.Update(gameTime);
            }
        }

        #endregion
    }
}
